import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * 功能：双向链表
 * 作者：zhoujun
 * 日期： 2025/2/5 22:12
 */
public class DoubleLinkedListSentinel implements Iterable<Integer>{

    private static class Node {
        Node prev;
        int value;
        Node next;

        public Node(Node prev, int value, Node next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    private Node head;
    private Node tail;

    public DoubleLinkedListSentinel() {
        head = new Node(null, 666, null);
        tail = new Node(null, 888, null);
        head.next = tail;
        tail.prev = head;
    }
        private Node findNode(int index) {
            int i = -1;
            for (Node p = head; p != tail; p = p.next, i++) {
                if (i == index) {
                    return p;
                }
            }
            return null;
        }

        public void addFirst(int value) {
            insert(0, value);
        }

        public void removeFirst() {
        remove(0);
        }

        public void addLast(int value) {
        Node last = tail.prev;
        Node added = new Node(last, value, tail);
        last.next = added;
        tail.prev = added;

        }

    public int get(int index) {
        Node p = findNode(index);
        if (p != null) {
            return p.value;
        } else {
            throw new IllegalArgumentException(
                    String.format("index [%d]", index));
        }
    }

        public void removeLast(int index) {
            Node removed = tail.prev;
            if (removed == head) {
                throw new IllegalArgumentException("不能删除头节点");
            }
            Node prev = removed.prev;
            prev.next = tail;
            tail.prev = prev;
        }

        public void insert(int index, int value) {
            Node prev = findNode(index - 1);
            if (prev == null) {
                throw new IllegalArgumentException("你输入的index不合法！");
            }
            Node next = prev.next;
            Node inserted = new Node(prev, value, next);
            prev.next = inserted;
            next.prev = inserted;
        }

        public void remove(int index) {
            Node prev = findNode(index - 1);
            if (prev == null) {
                throw new IllegalArgumentException();
            }
            Node removed = prev.next;
            if (removed == tail) {
                throw new IllegalArgumentException("不能删除尾节点");
            }
            Node next = removed.next;

            prev.next = next;
            next.prev = prev;
        }

    public void loop() {
        Node p = head.next;
        while (p != null) {
            System.out.println(p.value);
            p = p.next;
        }
    }

    public void loop1(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }






    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = head.next;
            @Override
            public boolean hasNext() {
                return p != tail;
            }

            @Override
            public Integer next() {
                int value = p.value;
                p = p.next;
                return value;
            }
        };
    }


}